﻿/* ----------------------------------------------------------------------------
 * Copyright (c) Philip Stears 
 *
 * This source code is subject to terms and conditions of the Microsoft Public 
 * License (Ms-PL).
 * 
 * By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 * 
 * To request a copy of the Microsoft Public License, send a request by email
 * to license@connectbasic.net.
 *
 * This notice must not be removed from this software.
 * ---------------------------------------------------------------------------- */

namespace ConnectBasic.ObjectModel
{
    using ConnectBasic.Execution;
    using DlrExpression = Microsoft.Linq.Expressions.Expression;

    /// <summary>
    /// Represents a constant definition.
    /// </summary>
    public sealed class ConstantStatement : StatementNodeBase
    {
        private readonly string mConstantName;
        private readonly UnboundType mConstantType;
        private readonly ExpressionBase mExpression;

        /// <summary>
        /// Initializes a new instance of the <see cref="ConstantStatement" /> class.
        /// </summary>
        /// <param name="constantName">The name of the constant (might be null if this is an error node).</param>
        /// <param name="type">The type of the constant.</param>
        /// <param name="expression">The expression which provides the value of the constant (might be null if this is an error node).</param>
        public ConstantStatement(string constantName, UnboundType type, ExpressionBase expression)
        {
            mConstantName = constantName;
            mConstantType = type;
            mExpression = expression;
        }

        /// <summary>
        /// Gets the name of the constant.
        /// </summary>
        public string ConstantName
        {
            get { return mConstantName; }
        }

        /// <summary>
        /// Gets the type of the constant.
        /// </summary>
        public UnboundType Type
        {
            get { return mConstantType; }
        }

        /// <summary>
        /// Gets the expression which provides the value of the constant.
        /// </summary>
        public ExpressionBase Expression
        {
            get { return mExpression; }
        }

        /// <inheritdoc />
        public override DlrExpression Compile(BasicCodeGenerator gen)
        {
            
            // TODO: Ahead-of-time calculation of the constant
            return GeneratorUtil.Convert(mExpression.Compile(gen), gen.ResolveType(mConstantType));
        }
    }
}
